package com.ccy.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ccy.product.config.GuliThreadPoolExcuter;
import com.ccy.product.dao.SkuInfoDao;
import com.ccy.product.entity.SkuImagesEntity;
import com.ccy.product.entity.SkuInfoEntity;
import com.ccy.product.entity.SpuInfoDescEntity;
import com.ccy.product.service.SkuImagesService;
import com.ccy.product.service.SkuInfoService;
import com.ccy.product.service.SkuSaleAttrValueService;
import com.ccy.product.service.SpuInfoDescService;
import com.ccy.product.vo.SkuItemSaleAttrVo;
import com.ccy.product.vo.SkuItemVo;
import com.ccy.product.vo.SpuItemAttrGroupVo;
import com.gulimall.common.utils.PageUtils;
import com.gulimall.common.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    private GuliThreadPoolExcuter threadPoolExcuter;


    @Autowired
    private SkuImagesService skuImagesService;

    @Autowired
    private SkuSaleAttrValueService skuSaleAttrValueService;


    @Autowired
    private SpuInfoDescService spuInfoDescService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> getSkusBySpuId(long spuId) {

        return this.list(new QueryWrapper<SkuInfoEntity>().eq("spu_id",spuId));

    }

    // 异步编排
    // 什么时候用异步？ 耗时操作【i/0操作（读数据库，读文件） 网络操作   httpclient】
    @Override
    public SkuItemVo getSkuInfoById(long skuId) throws ExecutionException, InterruptedException {


        SkuItemVo skuItemVo = new SkuItemVo();
        CompletableFuture<SkuInfoEntity> skuInfoFuture = CompletableFuture.supplyAsync(() -> {
            //1、sku基本信息的获取  pms_sku_info
            SkuInfoEntity skuInfoEntity = this.getById(skuId);
            skuItemVo.setInfo(skuInfoEntity);
            skuItemVo.setHasStock(true);
            return skuInfoEntity;
        });


        CompletableFuture<Void> skuImagesFuture = CompletableFuture.runAsync(() -> {
            //2、sku的图片信息    pms_sku_images
            List<SkuImagesEntity> skuImages = skuImagesService.list(new QueryWrapper<SkuImagesEntity>().eq("sku_id", skuId));
            skuItemVo.setImages(skuImages);
        });


        CompletableFuture<Void> attrGroupWithAttrsFuture = CompletableFuture.runAsync(() -> {
            //3、获取spu的销售属性组合
            List<SpuItemAttrGroupVo> attrGroupWithAttrsBySkuId = skuSaleAttrValueService.getAttrGroupWithAttrsBySkuId(skuId);
            skuItemVo.setGroupAttrs(attrGroupWithAttrsBySkuId);
        });

        CompletableFuture<Void> spuInfoDescFuture = skuInfoFuture.thenAcceptAsync((sku) -> {
            //4、获取spu的介绍
            SpuInfoDescEntity spuInfoDesc = spuInfoDescService.getById(sku.getSpuId());
            skuItemVo.setDesc(spuInfoDesc);
        });
        CompletableFuture<Void> allItemSaleAttrsFuture = CompletableFuture.runAsync(() -> {
            //5、获取spu的规格参数信息
            List<SkuItemSaleAttrVo> allItemSaleAttrs = skuSaleAttrValueService.getAllItemSaleAttrs(skuId);
            skuItemVo.setSaleAttr(allItemSaleAttrs);
        });


        CompletableFuture.allOf(skuImagesFuture,attrGroupWithAttrsFuture,spuInfoDescFuture,allItemSaleAttrsFuture).get();






        return skuItemVo;
    }

}